package lincen.javase.util.time;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具
 * @author 	林岑
 * @time	2012年11月1日 23:34:49
 *
 */
public class TimeUtil {

	/**
	 * 返回当前时间距离指定时间点的秒数
	 * @param	hour	小时数，24小时制
	 * 
	 * @return
	 */
	public static int getRemainSecondTo( int hour ){
		Calendar now = Calendar.getInstance();
		Calendar last = Calendar.getInstance();
		last.set( Calendar.HOUR_OF_DAY, hour - 1 );//如果是9点就要计算8:59:59秒，所以要－1
		last.set( Calendar.MINUTE, 59 );
		last.set( Calendar.SECOND, 59 );
		//Date d = new Date( s.getLastRewardTime() );
		//Date now = new Date( System.currentTimeMillis() );
		int result = (int) ((last.getTimeInMillis() - now.getTimeInMillis()) / 1000);
		if( result < 0 ){
			result += 24 * 60 * 60;
		}
		
		return result;
	}
	

	/**
	 * 
	 * 返回当前时间距离午夜12点的秒数
	 * 
	 * @return
	 */
	public static int getRemainSecondToday(){
		Calendar now = Calendar.getInstance();
		Calendar last = Calendar.getInstance();
		last.set( Calendar.HOUR_OF_DAY, 23 );
		last.set( Calendar.MINUTE, 59 );
		last.set( Calendar.SECOND, 59 );
		//Date d = new Date( s.getLastRewardTime() );
		//Date now = new Date( System.currentTimeMillis() );

		
		return (int) ((last.getTimeInMillis() - now.getTimeInMillis()) / 1000);
	}
	

	/**
	 * 判断t1日期和当前时间是否同一天<br>
	 * 鉴于游戏每个礼拜会重启一次，因此没判断年份是否相同
	 * 
	 * @param t1
	 * @return
	 */
	public static boolean isSameDay( Date t1 ){
		Calendar now = Calendar.getInstance();
		Calendar last = Calendar.getInstance();
		last.setTimeInMillis( t1.getTime() );
		//Date d = new Date( s.getLastRewardTime() );
		//Date now = new Date( System.currentTimeMillis() );

		if( last.get(Calendar.MONTH) == now.get(Calendar.MONTH) && last.get(Calendar.DATE) == now.get( Calendar.DATE) ){
			return true;
		}
		return false;
	}
}
